package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.entity.Resource;
import com.zzyl.service.ResourcesService;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeItemVo;
import com.zzyl.vo.TreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resource")
public class ResourcesController {
    @Autowired
    private ResourcesService resourcesService;

    @PostMapping("/list")
    public ResponseResult<List<ResourceVo>> findResourceList(@RequestBody ResourceDto dto){
        List<ResourceVo> voList = resourcesService.findResourceList(dto);
        return ResponseResult.success(voList);
    }

    @PostMapping("/tree")
    public ResponseResult<TreeVo> findResourceTree(@RequestBody TreeItemVo itemVo){
        TreeVo vo = resourcesService.findResourceTree(itemVo);
        return ResponseResult.success(vo);
    }

    @PutMapping
    public ResponseResult saveResource(@RequestBody ResourceDto resourceDto){
        Resource resource = BeanUtil.toBean(resourceDto, Resource.class);
        resourcesService.saveResource(resource);
        return ResponseResult.success();
    }
}
